package com.aaa.cars.service.impl;

import com.aaa.cars.dao.MaintainRecordDao;
import com.aaa.cars.entity.Car;
import com.aaa.cars.entity.MaintainRecord;
import com.aaa.cars.service.CarService;
import com.aaa.cars.service.MaintainRecordService;
import com.aaa.common.constants.ExceptionConstant;
import com.aaa.common.constants.ReturnStatus;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 维修记录表(MaintainRecord)表服务实现类
 *
 * @author makejava
 * @since 2020-12-04 19:28:05
 */
@Service("maintainRecordService")
@Transactional
public class MaintainRecordServiceImpl implements MaintainRecordService {
    @Resource
    private MaintainRecordDao maintainRecordDao;
    @Resource
    private CarService carService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public MaintainRecord queryById(Integer id) {
        return this.maintainRecordDao.queryById(id);
    }

    /**
     * 通过实体作为筛选条件查询
     *
     * @param pageNo
     * @param pageSize
     * @param maintainRecord 实例对象
     * @return 对象列表
     */
    @Override
    public Result queryAll(int pageNo, int pageSize, MaintainRecord maintainRecord) {
        if(pageNo==0||pageSize==0){
            throw new CustomException(ExceptionConstant.ID_NOT_NULL.getErrorCode(),
                    ExceptionConstant.ID_NOT_NULL.getErrorMessage());
        }
        PageHelper.startPage(pageNo,pageSize);
        PageInfo<MaintainRecord> pageInfo = new PageInfo<>(maintainRecordDao.queryAll(maintainRecord));
        return new Result(ReturnStatus.SUCCESS.getReturnCode(),
                ReturnStatus.SUCCESS.getReturnMsg(), pageInfo);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<MaintainRecord> queryAllByLimit(int offset, int limit) {
        return this.maintainRecordDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param maintainRecord 实例对象
     * @return 实例对象
     */
    @Override
    public MaintainRecord insert(MaintainRecord maintainRecord) {
        Car car = new Car();
        car.setId(maintainRecord.getCarId());
        car.setState(2);
        carService.update(car);
        this.maintainRecordDao.insert(maintainRecord);
        return maintainRecord;
    }

    /**
     * 修改数据
     *
     * @param maintainRecord 实例对象
     * @return 实例对象
     */
    @Override
    public MaintainRecord update(MaintainRecord maintainRecord) {
        this.maintainRecordDao.update(maintainRecord);
        return this.queryById(maintainRecord.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.maintainRecordDao.deleteById(id) > 0;
    }
}